/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "资源读写.h"

#include "物体管理/渲染物体创建销毁.h"
#include "物体管理/渲染物体管理.h"
#include "物体编辑/物体编辑.h"





C_资源异步加载保存::C_资源异步加载保存(uint32 累计次数) : m_计时("资源异步加载", 累计次数) {
	m_运行模型加载 = true;
	m_计时.begin();

	threads.push_back(std::thread([&](){
		for (; m_运行模型加载; ) {
			std::unique_lock<std::mutex> lock(m_模型加载锁);
			m_模型加载等待.wait(lock);
	
			m_计时.begin();
			for (auto it = m_待构建多边形物体.begin(); it != m_待构建多边形物体.end(); ) {
				if (m_计时.end() >= 200) {
					//f_window_触发消息(S_框架::g_主窗口.data(), S_框架::g_主窗口.size());
					break;
				}
	
				auto& ob = *it;
				f_polygon_构建边((S_多边形*)ob->m_多边形);
				f_polygon_构建多边形顶点关系((S_多边形*)ob->m_多边形);
				f_polygon_构建面与顶点关联((S_多边形*)ob->m_多边形);
				f_polygon_计算面法线((S_多边形*)ob->m_多边形);
				f_polygon_计算光滑法线((S_多边形*)ob->m_多边形);
				
				f_ui_添加信息框文本(S_框架::g_ui_加载保存信息框, u"构建物体多边形:" + ob->m_Name);

				m_待填充网格物体.insert(*it);
				it = m_待构建多边形物体.erase(it);

				
			}
		}
	}) );


}

C_资源异步加载保存::~C_资源异步加载保存() {
	m_运行模型加载 = false;

	for (auto& e : threads) {
		e.join();
	}
}

void C_资源异步加载保存::f_添加事件(std::function<std::string()>& event) {
	m_事件池.push_back(event);

	//std::thread 新线程(event);
	//threads.push_back(新线程);

}

void C_资源异步加载保存::f_添加加载物体(const std::set<S_物体*>& 待添加物体) {
	std::unique_lock<std::mutex> lock(m_模型加载锁);
	m_待构建多边形物体.insert(待添加物体.begin(), 待添加物体.end());
}

void C_资源异步加载保存::f_添加保存物体(const std::set<S_物体*>& 待保存物体) {
	
}

bool C_资源异步加载保存::f_轮询事件() {
	bool 状态 = false;
	bool 添加物体返回信息 = m_待构建多边形物体.size() || m_待填充网格物体.size();

	if (m_计时.end() >= 200 || m_待添加物体.size()) {
		
		m_计时.begin();
		if (添加物体返回信息) {
			if (m_模型加载锁.try_lock()) {

				for (auto it = m_待填充网格物体.begin(); it != m_待填充网格物体.end(); ) {
					if (m_计时.end() >= 200) {
						//f_window_触发消息(S_框架::g_主窗口.data(), S_框架::g_主窗口.size());
						break;
					}

					auto& ob = *it;
					f_OB_创建网格物体数据(*S_节点框架::g_3D视口环境, ob);
					auto& 面 = f_ob_get数据(ob, 0);
					auto& 边 = f_ob_get数据(ob, 1);

					f_surface_多边形生成网格((S_多边形*)ob->m_多边形, 面.m_Mesh, 边.m_Mesh);
					f_OE_更新网格包围盒({ob});
					
					f_ui_添加信息框文本(S_框架::g_ui_加载保存信息框, u"构建物体网格:" + ob->m_Name);

					m_待添加物体.insert(*it);
					it = m_待填充网格物体.erase(it);
				}

				m_模型加载锁.unlock();
				
			}
			m_模型加载等待.notify_one();
		}
		else {
			if (m_待添加物体.size()) {
				f_om_添加渲染物体(m_待添加物体);
				m_待添加物体.clear();
				f_ui_添加信息框文本(S_框架::g_ui_加载保存信息框, u"物体网格构建完成");

				S_框架::g_ui_加载保存信息框->mf_打开关闭(S_框架::g_ui_加载保存信息框, false);
				添加物体返回信息 = true;
			}
		}
		

	}

	return 添加物体返回信息;
}

void C_资源异步加载保存::f_打开信息框() {
	
}

void C_资源异步加载保存::f_构建物体网格() {
	
}














